package kateba.bluetooth;
import java.util.Set;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class DeviceListActivity extends Activity 
{
    protected static final String TAG = "TAG";
    private BluetoothAdapter mBluetoothAdapter;
    private static final int DISCOVERY_REQUEST = 1;
    private ArrayAdapter<String> mPairedDevicesArrayAdapter;
private static BroadcastReceiver broadcastReceiver;
    /**
 * @return the broadcastReceiver
 */
public static BroadcastReceiver getBroadcastReceiver() {
	return broadcastReceiver;
}

/**
 * @param broadcastReceiver the broadcastReceiver to set
 */
public void setBroadcastReceiver(BroadcastReceiver broadcastReceiver) {
	this.broadcastReceiver = broadcastReceiver;
}

	@Override
    protected void onCreate(Bundle mSavedInstanceState) 
    {
        super.onCreate(mSavedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.device_list);

        setResult(Activity.RESULT_CANCELED);
        mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);

        ListView mPairedListView = (ListView) findViewById(R.id.paired_devices);
        mPairedListView.setAdapter(mPairedDevicesArrayAdapter);
        mPairedListView.setOnItemClickListener(mDeviceClickListener);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        Set<BluetoothDevice> mPairedDevices = mBluetoothAdapter.getBondedDevices();

        if (mPairedDevices.size() > 0) 
        {
            findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
            for (BluetoothDevice mDevice : mPairedDevices) 
            {
                mPairedDevicesArrayAdapter.add(mDevice.getName() + "\n" + mDevice.getAddress());
            }
        } 
        else 
        {
            String mNoDevices = getResources().getText(R.string.none_paired).toString();
            mPairedDevicesArrayAdapter.add(mNoDevices);
        }
        //TODO:
     
       final BroadcastReceiver mReceiver = new BroadcastReceiver() 
        { 
            public void onReceive(Context context, Intent intent) 
            {
                String action = intent.getAction(); 
                // When discovery finds a device 
                if (BluetoothDevice.ACTION_FOUND.equals(action)) 
                {
                // Get the BluetoothDevice object from the Intent 
                BluetoothDevice device = intent.getParcelableExtra(
                BluetoothDevice.EXTRA_DEVICE);
                Log.v("BlueTooth Testing",device.getName() + "\n"
                + device.getAddress()); 
                mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                }
            }    
        };

//        String aDiscoverable = BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;
//        startActivityForResult(new Intent(aDiscoverable),DISCOVERY_REQUEST);
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
        registerReceiver(mReceiver, filter);
        setBroadcastReceiver(mReceiver);
        mBluetoothAdapter.startDiscovery();

    }

    @Override
    protected void onDestroy() 
    {
        super.onDestroy();
        if (mBluetoothAdapter != null) 
        {
            mBluetoothAdapter.cancelDiscovery();
        }
    }

    private OnItemClickListener mDeviceClickListener = new OnItemClickListener() 
    {
        public void onItemClick(AdapterView<?> mAdapterView, View mView, int mPosition, long mLong) 
        {
            mBluetoothAdapter.cancelDiscovery();
            String mDeviceInfo = ((TextView) mView).getText().toString();
            String mDeviceAddress = mDeviceInfo.substring(mDeviceInfo.length() - 17);
            Log.v(TAG, "Device_Address " + mDeviceAddress);

            Bundle mBundle = new Bundle();
            mBundle.putString("DeviceAddress", mDeviceAddress);
            Intent mBackIntent = new Intent();
            mBackIntent.putExtras(mBundle);
           
            setResult(Activity.RESULT_OK, mBackIntent);
            finish();
        }
    };

}

